package net.sunzc.base.utils;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;
import com.socks.library.KLog;
import net.sunzc.base.BaseApplication;
import net.xichiheng.yulewa.YLWapplication;

import java.util.Calendar;
import java.util.Locale;

/**
 * 视图方面的工具类，凡是跟显示相关的工具方法都写到这里面
 * Created by sunzc_000 on 2015/11/20.
 */
public class ViewUtils {
    private static BaseApplication app = BaseApplication.getInstance();

    /**
     * 不带数据页面跳转
     *
     * @param activity
     * @param targetActivity
     */
    public static void toActivity(Activity activity, Class targetActivity) {
        toActivity(activity, targetActivity, null);
    }

    /**
     * 携带数据页面跳转
     *
     * @param activity
     * @param targetActivity
     * @param data
     */
    public static void toActivity(Activity activity, Class targetActivity, Bundle data) {
        Intent jump = getJumpIntent(activity, targetActivity, data);
        activity.startActivity(jump);
        activity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    /**
     * 携带数据并要求有返回结果的页面跳转
     *
     * @param fromActivity
     * @param targetActivity
     * @param data
     * @param requestCode
     */
    public static void toActivityForResult(Activity fromActivity, Class targetActivity,
                                           int requestCode, Bundle data) {
        Intent intent = getJumpIntent(fromActivity, targetActivity, data);
        fromActivity.startActivityForResult(intent, requestCode);
    }

    /**
     * 不携带数据，要求有返回结果的页面跳转
     *
     * @param fromActivity
     * @param toActivityClass
     * @param requestCode
     */
    public static void toActivityForResult(Activity fromActivity, Class toActivityClass, int requestCode) {
        toActivityForResult(fromActivity, toActivityClass, requestCode, null);
    }

    private static Intent getJumpIntent(Activity fromActivity, Class toActivityClass,
                                        Bundle arguments) {
        Intent intent = new Intent(fromActivity, toActivityClass);
        if (arguments != null) {
            intent.putExtras(arguments);
        }
        return intent;
    }

    private static Toast mToast;

    /**
     * 显示Toast
     *
     * @param context  ct
     * @param content  msg
     * @param showTime Toast.LENGTH_LONG 长时间
     *                 Toast.LENGTH_SHORT 短时间
     */
    public static void showToast(Context context, String content, int showTime) {
        if (mToast != null) {
            mToast.setText(content);
        } else {
            mToast = Toast.makeText(context, content, showTime);
        }
        mToast.show();
    }

    public static void showToast(String content) {
        showToast(app.getApplicationContext(), content, Toast.LENGTH_LONG);
    }

    public static void showBirthdayDialog(Context context, DatePickerDialog.OnDateSetListener onDateSetListener) {
        Calendar date = Calendar.getInstance(Locale.CHINA);
        DatePickerDialog dialog = new DatePickerDialog(context,
                onDateSetListener,
                date.get(Calendar.YEAR),
                date.get(Calendar.MONTH),
                date.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }

    public static void showTimeDialog(Context context, TimePickerDialog.OnTimeSetListener onTimeSetListener) {
        Calendar date = Calendar.getInstance(Locale.CHINA);
        TimePickerDialog dialog = new TimePickerDialog(context,
                onTimeSetListener,
                date.get(Calendar.HOUR_OF_DAY),
                date.get(Calendar.MINUTE),
                true);
        dialog.show();
    }

    public static void showNoNetworkDialog(final Context context) {
//        final AlertDialog.Builder dialog = new AlertDialog.Builder (context);
//        dialog.setTitle ("呀,咋没网了...");
//        dialog.setCancelable (false);
//        dialog.setMessage ("网络连接断开了呢,快去设置");
//        dialog.setPositiveButton ("设置网络", new DialogInterface.OnClickListener () {
//            @Override
//            public void onClick (DialogInterface dialog, int which) {
//                context.startActivity (new Intent (Settings.ACTION_SETTINGS));
//                dialog.dismiss ();
//            }
//        });
//        dialog.setNegativeButton ("取消", new DialogInterface.OnClickListener () {
//            @Override
//            public void onClick (DialogInterface dialog, int which) {
//                dialog.dismiss ();
//            }
//        });
//        dialog.create ().show ();
        showToast("没网络了");
    }

    /**
     * 检查textview或者edittext中输入内容是否为空
     *
     * @param textView
     * @param content
     * @return
     */
    public static boolean inputIsEmpty(TextView textView, StringBuilder content, String error) {
        String inputStr = textView.getText().toString();
        if (content.capacity() > 0)
            content.delete(0, content.capacity());
        if (TextUtils.isEmpty(inputStr)) {
            textView.setError(error);
            textView.requestFocus();
            return false;
        } else {
            content.append(inputStr);
            return true;
        }
    }

    /**
     * 检查textview或者edittext中输入内容是否为空
     *
     * @param textView 手机号所在的Textview或者EditText
     * @param content  如果输入的内容是手机号则返回手机号
     * @return 如果输入的手机号不是手机号则返回false，反之则为true
     */
    public static boolean inputPhoneIsCorrect(TextView textView, StringBuilder content, String error) {
        String inputStr = textView.getText().toString();
        if (content.capacity() > 0)
            content.delete(0, content.capacity());
        if (FormatUtils.checkMobile(inputStr)) {
            content.append(inputStr);
            return true;
        } else {
            textView.setError(error);
            textView.requestFocus();
            return false;
        }
    }

    public static void hideKeyboard(TextView textView) {
        Context context = textView.getContext();
        InputMethodManager keyboardManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (keyboardManager.hideSoftInputFromWindow(textView.getWindowToken(), 0)) {
            KLog.i("隐藏了" + textView.toString());
        }

    }
}
